<?php

/**
 * This is the model class for table "tbl_pesanan_pembayaran".
 *
 * The followings are the available columns in table 'tbl_pesanan_pembayaran':
 * @property string $ID_Pesanan
 * @property string $ID_Pembayaran
 */
class PesananPembayaran extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return PesananPembayaran the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tbl_pesanan_pembayaran';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('ID_Pesanan, ID_Pembayaran', 'required'),
			array('ID_Pesanan, ID_Pembayaran', 'length', 'max'=>20),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('ID_Pesanan, ID_Pembayaran', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'ID_Pesanan' => 'ID Pesanan',
			'ID_Pembayaran' => 'ID Pembayaran',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('ID_Pesanan',$this->ID_Pesanan,true);
		$criteria->compare('ID_Pembayaran',$this->ID_Pembayaran,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}


        public function beforeValidate(){
            parent::beforeValidate();
            $valid1 = !PesananPembayaran::model()->exists('ID_Pesanan = :id' , array(':id'=>$this->ID_Pesanan));
            if($valid1 == false)
                $this->addError('ID_Pesanan','ID Pesanan has already payed');
            $valid2 = Pesanan::model()->exists('ID_Pesanan = :id' , array(':id'=>$this->ID_Pesanan));
            if($valid2 == false)
                $this->addError('ID_Pesanan','ID Pesanan does not exist');
            $valid3 = true;
            if(Yii::app()->user->isMember()){
                $valid3 = Pesanan::model()->exists('ID_Pesanan = :id and Username = :name' , array(':id'=>$this->ID_Pesanan, ':name'=>Yii::app()->user->name));
                if($valid3 == false){
                    $this->addError('ID_Pesanan','ID_Pesanan is not belong to you');
                }
            }
            return  $valid1 && $valid2 && $valid3;
        }
}